﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FVXCOMDIDOLib;

namespace Common
{
    public class FAST_IO
    {
        FvxDido m_dido = null;
        public static readonly FAST_IO instance = new FAST_IO();
        System.Timers.Timer tmr = new System.Timers.Timer(10);
        volatile ushort ReadData = 0;
        volatile ushort Trg = 0;
        volatile ushort Continue = 0;
        public event Action<ushort> TrigEvent;
        private FAST_IO()
        {
            m_dido = new FvxDidoClass();
            m_dido.DeviceType = DIDO_DEVICE_TYPE.deviceFIO01;
            m_dido.Open();
           
            tmr.Elapsed += tmr_Elapsed;
            tmr.Start();
        }

        void tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            tmr.Stop();
            ReadData = (ushort)(Read() & 0xffff);
            Trg = (ushort)(ReadData & (ReadData ^ Continue));
            if (Trg != 0 && TrigEvent != null)
            {
                TrigEvent(Trg);
            }
            tmr.Start();
        }
        public void WriteBit(int nBit, bool value)
        {
            m_dido.WriteBit(nBit, value);
        }
        public uint Read()
        {
            int value = 0;
            m_dido.Read(out value);
            return (uint)value;
        }
        public void Write(uint Value)
        {
            m_dido.Write((int)Value);
        }
        public bool ReadBit(uint nBit)
        { 
            bool value=false;
            m_dido.ReadBit((int)nBit, out value);
            return value;
        }
    }
}
